package algorithms.sorting

/**
 * [Counting Sort 1](https://www.hackerrank.com/challenges/countingsort1)
 */
// Complete the countingSort function below.
fun countingSort1(arr: Array<Int>): Array<Int> {
    // 票箱数组
    val ballot = Array(100) { 0 }

    arr.forEach { ballot[it]++ }

    return ballot
}

/**
 * [Counting Sort 2](https://www.hackerrank.com/challenges/countingsort2)
 */
// Complete the countingSort function below.
fun countingSort2(arr: Array<Int>): Array<Int> {
    // 票箱数组
    val ballot = Array(100) { 0 }
    arr.forEach { ballot[it]++ }
    val ans = Array(arr.size) { 0 }

    var i = 0
    ballot.forEachIndexed { index, it ->
        if (it > 0) {
            ans.fill(index, i, i + it)
            i += it
        }
    }
    return ans
}